package com.naicheng.ivote.views.fragments;

import java.util.List;

import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.naicheng.ivote.R;
import com.naicheng.ivote.doman.BaseResponseEntity;
import com.naicheng.ivote.doman.FindNewBean;
import com.naicheng.ivote.engin.BaseFragment;
import com.naicheng.ivote.parser.BaseParser;
import com.naicheng.ivote.uiself.ToastUtils;
import com.naicheng.ivote.views.FindGuessItemActivity;
import com.naicheng.ivote.volley.Response.Listener;

public class FindNewFragment extends BaseFragment {

	private ListView listView;
	private FindNewAdapter adapter;
	@Override
	protected View initViews(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		listView = new ListView(CTX);
		return listView;
	}

	String requestcommand = "newquizlist";
	protected void init() {
		listView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				FindNewBean item = adapter.getItem(position);
				// TODO 跳详情
				Intent i = new Intent(CTX, FindGuessItemActivity.class);
				i.putExtra("ID", item.getId());
				startActivity(i);
			}
		});
		
		getDataFromServer(requestcommand , null , new Listener<JSONObject>() {
			@Override
			public void onResponse(JSONObject response) {
				BaseResponseEntity<FindNewBean> entity = new BaseParser<FindNewBean>().parserCollectionList(response, FindNewBean.class);
				if(entity != null && entity.getState()){
					List<FindNewBean> data = entity.getData();
					adapter = new FindNewAdapter(data);
					listView.setAdapter(adapter);
				}else{
					ToastUtils.getNormalCenterToast(CTX, entity.getMsg());
				}
			}
		}, CTX);
	}
	
	
	class FindNewAdapter extends BaseAdapter{
		
		List<FindNewBean> data;
		
		public FindNewAdapter(List<FindNewBean> data) {
			super();
			this.data = data;
		}

		@Override
		public int getCount() {
			return data.size();
		}

		@Override
		public FindNewBean getItem(int position) {
			return data.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder = null;
			if(convertView == null){
				convertView = inflater.inflate(R.layout.find_item_new, null);
				holder = new ViewHolder();
				holder.tv_title = (TextView) convertView.findViewById(R.id.tv_title);
				holder.tv_content = (TextView) convertView.findViewById(R.id.tv_content);
				holder.tv_time = (TextView) convertView.findViewById(R.id.tv_time);
				convertView.setTag(holder);
			}else{
				holder = (ViewHolder) convertView.getTag();
			}
			FindNewBean item = getItem(position);
			holder.tv_title.setText(item.getTitle());
			holder.tv_content.setText(item.getContent());
			holder.tv_time.setText("截止时间："+item.getDeadline());
			return convertView;
		}
		
		
		class ViewHolder{
			TextView tv_title,tv_content,tv_time;
		}
	}

	
}
